package com.yiyi.web.controller.utils;

import com.yiyi.common.utils.StringUtils;
import com.yiyi.system.domain.CsVisitorStatistics;
import com.yiyi.system.service.ICsVisitorStatisticsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 定时任务调度测试
 *
 * @author yiyi
 */
@Component("yyTask")
public class YyTask
{
    @Autowired
    public RedisTemplate redisTemplate;

    @Autowired
    private ICsVisitorStatisticsService csVisitorStatisticsService;

    public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i)
    {
        System.out.println(StringUtils.format("执行多参方法： 字符串类型{}，布尔类型{}，长整型{}，浮点型{}，整形{}", s, b, l, d, i));
    }

    public void ryParams(String params)
    {
        System.out.println("执行有参方法：" + params);
    }

    // 批量新增访客
    public void ryNoParams() {
        String visitorKey = "visitor:logs";
        ListOperations<String, CsVisitorStatistics> listOps = redisTemplate.opsForList();
        List<CsVisitorStatistics> statisticsList = listOps.range(visitorKey, 0, -1);

        if (statisticsList != null && !statisticsList.isEmpty()) {
            // 批量保存到数据库
            int row = saveStatistics(statisticsList);

            // 清空 Redis 中的列表
            redisTemplate.delete(visitorKey);
            System.out.println("新增访客 " + row + " 影响行数！");
        } else {
            System.out.println("没有访客记录可新增！");
        }
    }

    // 批量插入统计数据
    private int saveStatistics(List<CsVisitorStatistics> statisticsList) {
        // 批量插入的方法
        return csVisitorStatisticsService.insertCsVisitorStatisticsBatch(statisticsList);
    }

}
